<?php

class Admin_EstateController extends Zend_Controller_Action
{	
    private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();
        
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;
        
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction()
    {       
        $modelEstate            = new Admin_Model_Estate();        
        $estateList             = $modelEstate->getAllData()->toArray();        
        $this->view->estateList = $estateList;
    }
    public function addAction()
    {
        $configApp = new Zend_Config_Ini(CONFIG_PATH . '/application.ini', APPLICATION_ENV);
        $this->view->configApp = $configApp;
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();                 
            if($formData){                
                
                if(isset($formData['estateName']))
                    $addData['estateName'] = $formData['estateName'];
                if(isset($formData['estateCode']))
                    $addData['estateCode'] = $formData['estateCode'];                
                if(isset($formData['otherInfoId']))
                    $addData['otherInfo'] = json_encode($formData['otherInfoId']);
                else
                    $addData['otherInfo'] = json_encode('');
                isset($formData['sale']) ? $addData['sale']='1' : $addData['sale']='0';
                isset($formData['rent']) ? $addData['rent']='1' : $addData['rent']='0';
                isset($formData['baseUtility']) ? $addData['baseUtility']='1' : $addData['baseUtility']='0';
                isset($formData['otherUtility']) ? $addData['otherUtility']='1' : $addData['otherUtility']='0';                                              
                                                                                                          
                if($addData['estateName'] && $addData['estateCode']){                    
                    $modelEstate = new Admin_Model_Estate();
                    $estateCheck=$modelEstate->getDataCheckUnique(array('estateName'=>$addData['estateName'],'estateCode'=>$addData['estateCode']));                    
                    if(!$estateCheck){                        
                        $estateId = $modelEstate->addData($addData);
                        if($estateId){
                            $this->_helper->redirector('index', 'estate', 'admin');
                        }else{
                            $this->view->mess = "Đã có lỗi xảy ra. Vui lòng thử lại!";
                            $this->view->flag = "error";
                       }
                    }else{
                        $this->view->mess = "Tên hoặc Mã bất động sản đã tồn tại. Vui lòng chọn tên khác!";
                        $this->view->flag = "error";
                    }
                }else{
                    $this->view->mess = "Vui lòng điền đẩy đủ thông tin!";
                    $this->view->flag = "error";
                }
            }else{
                $this->_helper->redirector('index', 'estate', 'admin');
            }
        }
        
        // mảng tất cả resource
        $modelResource  = new Admin_Model_Otherinfo();        
        $otherInfo      =$modelResource->getAllData();                               
        $this->view->otherInfo      = $otherInfo;                
    }
    public function editAction()
    {
        $configApp = new Zend_Config_Ini(CONFIG_PATH . '/application.ini', APPLICATION_ENV);
        $this->view->configApp = $configApp;
        $estateId = (int)($this->_getParam('id', 0));
        $modelEstate = new Admin_Model_Estate();
        $estateCheck = $modelEstate->getDataByEstateId($estateId);        
        $this->view->estateCheck = $estateCheck;        
        if($estateCheck){            
            if($this->getRequest()->isPost()){
                $formData = $this->getRequest()->getPost();                
                if($formData){
                    if(isset($formData['estateName']))
                    $updateData['estateName'] = $formData['estateName'];
                if(isset($formData['estateCode']))
                    $updateData['estateCode'] = $formData['estateCode'];                
                if(isset($formData['otherInfoId']))
                    $updateData['otherInfo'] = json_encode($formData['otherInfoId']);
                else
                    $updateData['otherInfo'] = json_encode('');
                isset($formData['sale']) ? $updateData['sale']='1' : $updateData['sale']='0';
                isset($formData['rent']) ? $updateData['rent']='1' : $updateData['rent']='0';
                isset($formData['baseUtility']) ? $updateData['baseUtility']='1' : $updateData['baseUtility']='0';
                isset($formData['otherUtility']) ? $updateData['otherUtility']='1' : $updateData['otherUtility']='0';                                                                                                                                                                       
                if($updateData['estateName'] && $updateData['estateCode']){                    
                    $modelEstate = new Admin_Model_Estate();
                    $estateCheck=$modelEstate->getDataCheckUnique(array('estateName'=>$updateData['estateName'],'estateCode'=>$updateData['estateCode']));                    
                    if(!$estateCheck || $estateCheck['estateId'] == $estateCheck['estateId']){                        
                        $modelEstate->updateData($estateId,$updateData);                        
                        $this->_helper->redirector('index', 'estate', 'admin');                        
                    }else{
                        $this->view->mess = "Tên hoặc Mã bất động sản đã tồn tại. Vui lòng chọn tên khác!";
                        $this->view->flag = "error";
                    }
                }else{
                    $this->view->mess = "Vui lòng điền đẩy đủ thông tin!";
                    $this->view->flag = "error";
                }
                }else{
                    $this->_helper->redirector('index', 'role', 'admin');
                }
            }else{
                // mảng tất cả resource                
                $modelOtherInfo = new Admin_Model_Otherinfo();        
                $infoList      = $modelOtherInfo->getAllData();                               
                $this->view->infoList = $infoList;
            }
        }else{
            $this->_helper->redirector('index', 'estate', 'admin');
        }
    }
    public function deleteAction()
    {
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){                
                $estateId = $this->getRequest()->getPost('estateId');
                $modelEstate = new Admin_Model_Estate();                                
                $modelEstate->deleteData($estateId);                
                // ghi log
            }
            $this->_helper->redirector('index', 'estate', 'admin');
        }else{
            $id = $this->_getParam('id', 0);
            $modelEstate = new Admin_Model_Estate();
            $this->view->estateCheck = $modelEstate->getDataByEstateId($id);
        }
    }
    
    public function demoAction(){
        $modelInfo=new Admin_Model_Otherinfo();
        $otherInfo=$modelInfo->getAllData();
        $this->view->otherInfo=$otherInfo;
    }
    
    public function getestateAction(){
        $transaction = $this->_request->getParam('transaction');                        
        $modelEstate=new Admin_Model_Estate;
        $estateTemp=$modelEstate->getDataByTransaction($transaction);
        foreach($estateTemp as $estate){
            $estateList[$estate[estateId]]=$estate[estateName];
        }        
        echo json_encode($estateList);        
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);        
    }
    public function getutilityAction(){
        $estate = $this->_request->getParam('estate');                                        
        $modelEstate=new Admin_Model_Estate();
        $utilityTemp=$modelEstate->getDataByEstateId($estate);
        $utilityList[baseUtility]=$utilityTemp[baseUtility];
        $utilityList[otherUtility]=$utilityTemp[otherUtility];                
        $infoId=spliti(',',str_replace(array('[',']','","','"'), array('','',',',''),$utilityTemp[otherInfo]));
        $modelInfo=new Admin_Model_Otherinfo();
        $otherInfo=$modelInfo->getOtherInfoDataById($infoId);                                                                        
        foreach($otherInfo as $item){
            $show[]=$item[colMap];
        }
        foreach($modelInfo->getAllData() as $item){
            if($item['groupInfo']=='otherInfo'){
                if(!in_array($item['colMap'],$show)){
                    $hide[]=$item['colMap'];
                }
            }
        }        
        $utilityList[otherInfoHide]=$hide;
        $utilityList[otherInfoShow]=$show;
        echo json_encode($utilityList);        
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
    }
    
   
    
    
}







